package lanou.a36ke_01.activity.newsactivity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import lanou.a36ke_01.R;
import lanou.a36ke_01.adapters.newsadapter.NewsAdapter;
import lanou.a36ke_01.beanlists.newsbean.NewsBean;
import lanou.a36ke_01.tools.BaseActivity;
import lanou.a36ke_01.tools.MyApp;
import lanou.a36ke_01.tools.NetTool;


public class SearchActivity extends BaseActivity implements TextWatcher, View.OnClickListener, AdapterView.OnItemClickListener {

    private String url = "https://rong.36kr.com/api/mobi/news/search?keyword=";
    private String urlString = "&page=1&pageSize=3";
    private EditText searchEt;
    private ImageView searchIv;
    private TextView searchTv;
    private ImageView background;
    private TextView tv;
    private String searchAty;
    private ListView listView;

    @Override
    protected int setLayout() {
        return R.layout.activity_search;
    }

    @Override
    protected void initView() {

        listView = bindView(R.id.activity_search_list_view);
        searchIv = bindView(R.id.activity_search_image);
        searchEt = bindView(R.id.activity_search_edit);
        searchTv = bindView(R.id.activity_search_text);
        background = bindView(R.id.activity_search_background);
        tv = bindView(R.id.activity_search_tv);
        searchTv.setOnClickListener(this);

        //为EditText添加输入文字后的监听

        searchEt.addTextChangedListener(this);
        listView.setOnItemClickListener(this);


    }

    @Override
    protected void initDate() {

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }


    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    //在文字改变之后执行的方法
    @Override
    public void afterTextChanged(Editable s) {

        searchAty = searchEt.getText().toString();
        String searchUrl = "https://rong.36kr.com/api/mobi/news/search?keyword=";
        String searchCondition = "&page=1&pageSize=20";


        String urll = searchUrl + searchAty + searchCondition;

        if (s.length() > 0) {

            background.setVisibility(View.GONE);
            tv.setVisibility(View.GONE);
            listView.setVisibility(View.VISIBLE);
            NetTool netTool = new NetTool();
            netTool.getNetData(urll, NewsBean.class, new NetTool.NetListener<NewsBean>() {
                @Override
                public void onSuccess(NewsBean newsBean) {
                    NewsAdapter newsAdapter = new NewsAdapter(SearchActivity.this);
                    Log.d("SearchActivity", "newsBean:" + newsBean);
                    newsAdapter.setNewsBean(newsBean);
                    listView.setAdapter(newsAdapter);
                }
                @Override
                public void onError(String errorMsg) {

                }
            });
        } else {
            background.setVisibility(View.VISIBLE);
            tv.setVisibility(View.VISIBLE);
            listView.setVisibility(View.GONE);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.activity_search_text:
                finish();
                break;
            case R.id.activity_search_image:
                String searchAry = searchEt.getText().toString();
                //判断edit时候有数据 如果有则隐藏图标以及无搜索记录
                if (searchEt != null){
                    background.setVisibility(View.GONE);
                    tv.setVisibility(View.GONE);
                } else {
                    background.setVisibility(View.VISIBLE);
                    tv.setVisibility(View.VISIBLE);
                }
                NetTool netTool = new NetTool();
                netTool.getNetData(url + searchAty + urlString, NewsBean.class, new NetTool.NetListener<NewsBean>() {
                    @Override
                    public void onSuccess(NewsBean newsBean) {
                        NewsAdapter newsAdapter = new NewsAdapter(SearchActivity.this);
                        Log.d("SearchActivity", "newsBean:" + newsBean);
                        newsAdapter.setNewsBean(newsBean);
                        listView.setAdapter(newsAdapter);

                    }

                    @Override
                    public void onError(String errorMsg) {

                    }
                });

                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, final int position, final long id) {

        searchAty = searchEt.getText().toString();
        String searchUrl = "https://rong.36kr.com/api/mobi/news/search?keyword=";
        String searchCondition = "&page=1&pageSize=20";

        String urll = searchUrl + searchAty + searchCondition;
        NetTool netTool = new NetTool();
        netTool.getNetData(urll, NewsBean.class, new NetTool.NetListener<NewsBean>() {
            @Override
            public void onSuccess(NewsBean newsBean) {

                String feedId = newsBean.getData().getData().get(position).getFeedId();
                Intent intent = new Intent(SearchActivity.this,NewsChildActivity.class);

                intent.putExtra("newsFeelId",feedId);

                startActivity(intent);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
    }
}
